//
//  JDBaseVCExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2024/3/4.
//

import UIKit

class JDBaseVCExtension: NSObject {

}

// MARK: pickerView
extension JDBaseViewController {
    
    func initOnePickerView() {
        self.onePickerView = UIPickerView()
        self.view.addSubview(self.onePickerView!)
        self.onePickerView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
            make.height.equalTo(150)
        })
        self.onePickerView?.backgroundColor = .white
        self.onePickerView?.delegate = self
        
        self.pickerViewTopToolBar = UIView()
        self.view.addSubview(self.pickerViewTopToolBar!)
        self.pickerViewTopToolBar?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.onePickerView!.snp.top)
            make.height.equalTo(50)
        })
        self.pickerViewTopToolBar?.backgroundColor = .white
        
        
//        var pickerViewBgView: UIView?
//        var pickerViewConfirmButton: UIButton?
//        var pickerViewCancelButton: UIButton?
        
        self.pickerViewConfirmButton = UIButton(type: .custom)
        self.pickerViewTopToolBar?.addSubview(self.pickerViewConfirmButton!)
        self.pickerViewConfirmButton?.snp.makeConstraints({ make in
            make.centerY.equalTo(self.pickerViewTopToolBar!)
            make.right.equalTo(self.pickerViewTopToolBar!).offset(-22)
        })
        self.pickerViewConfirmButton?.setTitle("确定", for: .normal)
        self.pickerViewConfirmButton?.addTarget(self, action: #selector(confirmButtonAction(_:)), for: .touchUpInside)
        self.pickerViewConfirmButton?.setTitleColor(color("#333333"), for: .normal)
        self.pickerViewConfirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        
        self.pickerViewCancelButton = UIButton(type: .custom)
        self.pickerViewTopToolBar?.addSubview(self.pickerViewCancelButton!)
        self.pickerViewCancelButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.pickerViewTopToolBar!).offset(22)
            make.centerY.equalTo(self.pickerViewTopToolBar!)
        })
        self.pickerViewCancelButton?.setTitle("取消", for: .normal)
        self.pickerViewCancelButton?.addTarget(self, action: #selector(pickerViewCancelAction), for: .touchUpInside)
        self.pickerViewCancelButton?.setTitleColor(color("#8D9198"), for: .normal)
        self.pickerViewCancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        
        self.pickerViewBgView = UIView()
        self.view.addSubview(self.pickerViewBgView!)
        self.pickerViewBgView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.pickerViewTopToolBar!.snp.top)
            make.top.equalTo(self.view)
        })
        self.pickerViewBgView?.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(pickerViewCancelAction))
        self.pickerViewBgView?.addGestureRecognizer(tap)
    }
    
    @objc func confirmButtonAction(_ currentButton: UIButton) {
//        selectedCompleted
        
        self.pickerViewCancelAction()
        let selectedIndex = self.onePickerView?.selectedRow(inComponent: 0) ?? 0
        self.onePickerViewCompleted?(selectedIndex)
        
//        let selectedStr = self.onePickerViewDataSource[selectedIndex]
//        printLog("选择完成:\(selectedStr)")
        
    }
    
    @objc func pickerViewCancelAction() {
        printLog("取消")
        self.onePickerView?.removeFromSuperview()
        self.pickerViewTopToolBar?.removeFromSuperview()
        self.pickerViewBgView?.removeFromSuperview()
    }
}

extension JDBaseViewController: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == self.onePickerView {
            return self.onePickerViewDataSource.count
        }
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        if pickerView == self.onePickerView {
            return self.onePickerViewDataSource[row] ?? ""
        }
        return "内容"+"\(row)"
    }
}

extension JDBaseViewController {
    
    func setupTableViewEmptyView(_ scrollView: UIScrollView?, offsetY: CGFloat = 50, emptyColor: UIColor = .commonColor()) {
        return;
//        let emptyV:HDEmptyView = HDEmptyView.emptyActionViewWithImageStr(imageStr: "emptyView", titleStr: NSString(string: "暂无内容"), detailStr: "", btnTitleStr: "") {
//        }
//        emptyV.contentViewY = offsetY
//        emptyV.imageSize = CGSize(width: 200, height: 200)
//        emptyV.titleLabFont = UIFont.systemFont(ofSize: 14, weight: .regular)
//        emptyV.titleLabTextColor = color("#8D9198")
//        emptyV.backgroundColor = emptyColor 
//        scrollView?.ly_emptyView = emptyV
//        scrollView?.ly_emptyView?.tapContentViewBlock = {
//             
//        }
//        scrollView?.ly_showEmptyView()
    }
    
    
}
